import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';
declare var $: any;
@Component({
	selector: 'sa-device-state',
	templateUrl: './device-state.component.html'
})
export class DeviceStateComponent implements OnInit {
	tableData = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	table:any = {
		selectedDeviceId: '',
		selectedCarNumber: '',
		selectedCompanyId: '',
		selectedCompanyName: '',
		searchKey: '',
		questionType: '',
		questionDesc: '',
	}
	startTime: string = '';
    endTime: string = '';
	deviceStatusList:any = [{
		value:'',
		label:'全部'
	},{
		value:0,
		label:'库存'
	},{
		value:1,
		label:'已发货'
	}];
	constructor(private jsonApiService: JsonApiService, private router: Router,private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage||1;
		});
	}
	ngOnInit() {
		this.load();
	}
	//table刷新
	refresh(){
    	this.load();
   	}
	//表格搜索
	table_search() {
		this.curPage = 1;
		this.load();
	}
	// 故障类型回调
	questionTypeChange(){
	    this.table.questionDesc  = '';
	}
	//初始化加载list数据
	load() {
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				deviceId:this.table.selectedDeviceId,
				carNumber:this.table.selectedCarNumber,
				companyId:this.table.selectedCompanyId,
				companyName:this.table.selectedCompanyName,
				searchKey:common.stringTrim(this.table.searchKey),
				questionType: this.table.questionType,
				str: this.table.questionDesc,
				startTime:this.startTime,
				endTime:this.endTime,
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("deviceMark/deviceMarkList.html", reqdata).subscribe(data => {
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//公司选中回调
    companySelected(data) {
		this.table.selectedDeviceId = data.deviceId;
		this.table.selectedCarNumber = data.carNumber;
		this.table.selectedCompanyId = data.companyId;
		this.table.selectedCompanyName = data.companyName;
	}
	//导出
	export_data() {
		var parameters = {
			deviceId:this.table.selectedDeviceId,
			carNumber:this.table.selectedCarNumber,
			companyId:this.table.selectedCompanyId,
			companyName:this.table.selectedCompanyName,
			searchKey:common.stringTrim(this.table.searchKey),
			questionType: this.table.questionType,
			str: this.table.questionDesc,
			startTime:this.startTime,
			endTime:this.endTime
		};
		let timeCheck = common.correctTime(parameters)
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		common.exprotDataFile(parameters, 'param', config.BaseURL + 'deviceMark/exportDeviceStateFile.html')
	}
	// 接收子组件传递过来的数据 
    getSelectTableTime(time) {
        this.startTime = time.startTime;
        this.endTime = time.endTime;
    }
	//查看
	ProblemModalTableData : any =[];
	problemName : any =''
	tablelook(type,row,modal){
		this.ProblemModalTableData = [];
		if(type == '1'){
			this.ProblemModalTableData = row;
			this.problemName = '传感器';
		}
		if(type =='2'){
			this.ProblemModalTableData.push(row);
			this.problemName = 'GPS';
		}
		modal.show();
	}
	cancelProblemModal(modal){
		this.ProblemModalTableData = [];
		modal.hide();
	}







	abnormalDeviceList: any =[];
    abnormalCarNamber:any;
    abnormalompanyName:any;
    /**
	 *  echart图   异常数据折线图
	 */
    specifyId={
		picker1:'datetimepicker3',
		picker2:'datetimepicker4'
	}
	activeSelected: any;
	startTime_chart: string = '';
	endTime_chart: string = '';
	isChartView: boolean = false;
	isMapView: boolean = false;
	specifyTime: any = {
		startTime: '',
		endTime: ''
	}
	selectedRow: any;
	drawTrackArr: any;
	//数据查看
	drawAbnormalChart() {
		if (!this.selectedRow) {
			this.jsonApiService.message('先选择需要查看折线图的车辆');
			return;
		}
		if (!this.startTime_chart || !this.endTime_chart) {
			this.jsonApiService.message('时间不能为空');
			return;
		}
		let timeCheck = common.correctTimeParams(this.startTime_chart, this.endTime_chart, 0);
		if (!timeCheck.status) {
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		this.lineParamDeal(this.selectedRow);
	}
	checkAbnormal(row: any) {
        this.abnormalDeviceList = [];
        var Problem = {}
        for(var k=0;k< this.tableData.length;k++){
            if(this.tableData[k].id == row.id){
                this.tableData[k].isChecked = true
            }else{
                this.tableData[k].isChecked = false
            }
        }
        if(row.sensorProblemList && row.sensorProblemList.length > 0){
            for(var i=0;i<row.sensorProblemList.length;i++){
                Problem ={
                    "problem":row.sensorProblemList[i].sensorProblem,
                    "startTime": row.sensorProblemList[i].startTime,
                    "endTime": row.sensorProblemList[i].endTime,
                    "type": 1,
                    "isActive": false,
                    "deviceId": row.deviceId
                }
                this.abnormalDeviceList.push(Problem);
            }
            
        }
        if(row.gpsProblemEntity&& row.gpsProblemEntity.gpsProblem){
            var problemName = "GPS-" + row.gpsProblemEntity.gpsProblem
            Problem ={
                "problem": problemName,
                "startTime": row.gpsProblemEntity.startTime,
                "endTime": row.gpsProblemEntity.endTime,
                "type": 2,
                "isActive": false,
                "deviceId": row.deviceId
            }
            this.abnormalDeviceList.push(Problem);
        }
        this.abnormalCarNamber = row.carNumber;
        this.abnormalompanyName = row.companyName;
        this.abnormalDeviceList[0].isActive = true;
        console.log(this.abnormalDeviceList);

		// row对象新增属性
		this.activeSelected = row.id;
		this.startTime_chart = this.abnormalDeviceList[0].startTime;
		this.endTime_chart = this.abnormalDeviceList[0].endTime;
		this.isChartView = true;
		this.specifyTime = {
			startTime: this.startTime_chart,
			endTime: this.endTime_chart,
		}
		this.lineParamDeal(row);
	}
	runParent(value) {
		this.drawTrackArr = value;
	}
	//折线图时间回调事件
	getSelectChartTime(time) {
		this.startTime_chart = time.startTime;
		this.endTime_chart = time.endTime;
	}
	showMapView(val) {
		this.isMapView = val;
	}
	//获取传感器通道参数
	lineParamDeal(row) {
		let reqdata = 'deviceId=' + row.deviceId;
		this.jsonApiService.http("realtime/getRealTimeData.html", reqdata).subscribe(result => {
			if (result.success && result.data) {
				let params = {
					deviceId: row.deviceId,
					startTime: this.startTime_chart,
					endTime: this.endTime_chart,
					coefficient: result.data.coef,
				}
				this.selectedRow = params;
			}
		})
	}
	closeChart() {
        this.isChartView = false;
        for(var k=0;k< this.tableData.length;k++){
            this.tableData[k].isChecked = false
        }
    }
    DeviceListClick(item){
        for(var i=0;i<this.abnormalDeviceList.length;i++){
            if(item.problem == this.abnormalDeviceList[i].problem){
                this.abnormalDeviceList[i].isActive = true;
                this.startTime_chart = this.abnormalDeviceList[i].startTime;
                this.endTime_chart = this.abnormalDeviceList[i].endTime;
                this.specifyTime = {
                    startTime: this.startTime_chart,
                    endTime: this.endTime_chart,
                }
                this.lineParamDeal(this.abnormalDeviceList[i]);
            }else{
                this.abnormalDeviceList[i].isActive = false;
            }
        }
    }
}